<?php
class UsersController extends AppController {

	var $name = 'Users';
	var $helpers = array('Html', 'Form');

	function beforeRender(){
		parent::beforeRender();
		$this->set('menu_parent', 'Users');			
		$this->set('menu_subactive', 'Users');		
	}
	
 	function login(){
   		$this->layout = 'admin_login';
   		$this->AuthExtension->checkRememberMe(); 
    }
    
    function admin_login(){
		$this->redirect(array('controller'=>'users','action'=>'login','admin'=>0));
		exit();	
    }
	
    function logout(){
    	 	
        $this->Session->del('Permissions');
        $this->AuthExtension->logout();   
       	$this->redirect($this->Auth->logout());
    }
    
    function admin_logout(){
    	//$this->AuthExtension->logout(); 
        $this->Session->del('Permissions');
        $this->AuthExtension->logout(); 	      
        $this->redirect($this->Auth->logout());
    }    


	function admin_index() { 
		$this->paginate = array('limit' => 5);
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

	function admin_view($id = null) {
		$this->Redirect->idEmpty($id, array('action' => 'index'));
		$this->set('user', $this->User->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->User->create();
			if ($this->User->save($this->data)) {
				$this->Redirect->flashSuccess('User Saved.', array('action' => 'view', $this->User->id));
			} else {
			  $this->Redirect->flashWarning('User could not be saved. Please try again.');
			}
		}
		$groups = $this->User->Group->find('list');
		$this->set(compact('groups'));
	}

	function admin_edit($id = null) {
		$this->Redirect->idEmpty($id, array('action' => 'index'));
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Redirect->flashSuccess('User Saved.', array('action' => 'view', $this->User->id));
			} else {
			  $this->Redirect->flashWarning('User could not be saved. Please try again.');
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
		$groups = $this->User->Group->find('list');
		$this->set(compact('groups'));
	}

	function admin_delete($id = null) {
		$this->Redirect->idEmpty($id, array('action' => 'index'));
		if ($this->User->del($id)) {
			$this->Redirect->flashSuccess('User Deleted.', array('action' => 'index'));
		}
		$this->autoRender = false;
	}
	
	
	function admin_search() {
		$this->Search->doSearch($this,$this->passedArgs);	
	}

	function admin_dataSearch() {
		$this->Search->redirectDataSearch($this,$this->data);
	}	
	

}
?>